package com.uchengwang.ui.cm_ui.me.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.SwitchCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.litesuits.orm.db.model.ColumnsValue;
import com.uchengwang.R;
import com.uchengwang.functions.db.manager.GroupDBManager;
import com.uchengwang.functions.db.manager.LiteOrmDBUtil;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.cycle.bean.RyGroupBean;
import com.uchengwang.ui.cm_ui.rychat.manager.RyGroupManager;
import com.uchengwang.ui.utils.ImageManagerUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.List;

import io.rong.imkit.RongIM;
import io.rong.imlib.model.Group;

/**
 * Created by U城网 on 2017/8/21.
 * 群信息 可编辑
 */

public class GroupInfoEditorActivity extends BaseWithTitleActivity implements View.OnClickListener{

    private ImageView editor_logo;
    private TextView editor_name;
    private RelativeLayout editor_introduce_rl, editor_logo_rl, editor_name_rl;
    private TextView editor_introduce;
    private RyGroupBean groupBean;

    private SwitchCompat editor_switch;

    private String groupLogo, groupName, groupDesc;
    private long groupId;
    private boolean isInit = true;//信息初始化
    private String ossHeadUrl;//上传oss的图片地址
    private TextView editor_change;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_editor_group);
        setTitle("群信息");

        groupBean = (RyGroupBean) getIntent().getSerializableExtra(BaseConfig.BEAN);


        initView();

    }

    private void initView() {
        editor_logo_rl = (RelativeLayout) findViewById(R.id.editor_logo_rl);
        editor_logo = (ImageView) findViewById(R.id.editor_logo);
        editor_change = (TextView) findViewById(R.id.editor_change);

        editor_name_rl = (RelativeLayout) findViewById(R.id.editor_name_rl);


        editor_name = (TextView) findViewById(R.id.editor_name);
        editor_introduce_rl = (RelativeLayout) findViewById(R.id.editor_introduce_rl);
        editor_introduce = (TextView) findViewById(R.id.editor_introduce);

        editor_switch = (SwitchCompat) findViewById(R.id.editor_switch);
        editor_switch.setEnabled(false);


        if (groupBean != null && groupBean.getAdminUid() == UCommonUtil.getUtil().getId()){
            //自己是群主
            editor_name_rl.setOnClickListener(this);

            editor_logo_rl.setOnClickListener(this);
            editor_introduce_rl.setOnClickListener(this);
            editor_change.setVisibility(View.VISIBLE);
            editor_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, final boolean b) {
                    if (!isInit){
                        MapRequest map = new MapRequest();
                        map.put(BaseConfig.ID, groupId);
                        map.put(BaseConfig.IS_NEED_APPLY, b);
                        RyGroupManager.getManager().changeGroupInfo(GroupInfoEditorActivity.this, map, new OnResultListener() {
                            @Override
                            public void onResult(int stutas, String msg, int total, String data) {
                                if (stutas ==0){
                                    ColumnsValue v = new ColumnsValue(new String[]{"isNeedApply"}, new Object[]{b});
                                    GroupDBManager.getManager().updateGroupInfo(GroupInfoEditorActivity.this, groupId, v);

                                    UCommonUtil.getUtil().showToast(GroupInfoEditorActivity.this, "修改成功");
                                }else {
                                    UCommonUtil.getUtil().showToast(GroupInfoEditorActivity.this, "修改失败");
                                }
                            }
                        });
                    }
                }
            });
            editor_switch.setEnabled(true);
            editor_switch.setChecked(groupBean.isNeedApply());
            isInit = false;

            setRightText("确定", new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (TextUtils.isEmpty(ossHeadUrl)){
                        GroupInfoEditorActivity.this.finish();
                        return;
                    }

                    MapRequest map = new MapRequest();
                    map.put(BaseConfig.ID, groupId);
                    map.put(BaseConfig.GROUP_LOGO, ossHeadUrl);
                    RyGroupManager.getManager().changeGroupInfo(GroupInfoEditorActivity.this, map, new OnResultListener() {
                        @Override
                        public void onResult(int stutas, String msg, int total, String data) {
                            if (stutas ==0){
                                ColumnsValue v = new ColumnsValue(new String[]{"groupLogo"}, new Object[]{ossHeadUrl});
                                GroupDBManager.getManager().updateGroupInfo(GroupInfoEditorActivity.this, groupId, v);

                                List<RyGroupBean> list = LiteOrmDBUtil.getLiteOrm(GroupInfoEditorActivity.this).getQueryByWhere(RyGroupBean.class, "id", String.valueOf(groupId));
                                if (list != null && list.size() >0){
                                    RyGroupBean groupBean = list.get(0);
                                    Group group = new Group(String.valueOf(groupId), ossHeadUrl, Uri.parse(groupBean.getGroupLogo()));
                                    RongIM.getInstance().refreshGroupInfoCache(group);
                                }

                                UCommonUtil.getUtil().showToast(GroupInfoEditorActivity.this, "修改成功");
                                GroupInfoActivity.isDeleteMember = true;
                            }else {
                                UCommonUtil.getUtil().showToast(GroupInfoEditorActivity.this, "修改失败");
                            }
                        }
                    });
                }
            });
        }

        initData();

    }

    private void initData(){
        if (groupBean != null){
             groupLogo = groupBean.getGroupLogo();
             groupName = groupBean.getGroupName();
             groupDesc = groupBean.getGroupDesc();
            groupId = groupBean.getId();

            if (!TextUtils.isEmpty(groupLogo)){
                ImageManager.getInstance().loadImage(this, editor_logo, groupLogo, ImageManager.ImageType.ROUND);
            }

            editor_name.setText(groupName);
            editor_introduce.setText(groupDesc);
            editor_switch.setChecked(groupBean.isNeedApply());

        }

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.editor_logo_rl://修改群头像
                chooseImage();
                break;

            case R.id.editor_name_rl://修改群昵称
                Intent intent = new Intent(this, EditorInfoActivity.class);
                intent.putExtra(BaseConfig.TYPE_STRING, groupName);
                intent.putExtra(BaseConfig.TYPE, 15);
                intent.putExtra(BaseConfig.ID, groupId);
                startActivityForResult(intent, 0);

                break;
            case R.id.editor_introduce_rl://修改群描述
                Intent intent1 = new Intent(this, EditorInfoActivity.class);
                intent1.putExtra(BaseConfig.TYPE_STRING, groupDesc);
                intent1.putExtra(BaseConfig.TYPE, 16);
                intent1.putExtra(BaseConfig.ID, groupId);
                startActivityForResult(intent1, 1);
                break;
        }
    }

    /**
     * 选择图片
     * */
    private void chooseImage(){
        UCommonUtil.getUtil().openAndCrop(this, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
            }

            @Override
            public void onResp(final int status, String msg, String data) {
                Log.d("jjf", "裁剪的地址="+data);
                String headUrl = data;
                if (!TextUtils.isEmpty(headUrl)){
                    ImageManager.getInstance().loadImage(GroupInfoEditorActivity.this, editor_logo, headUrl, ImageManager.ImageType.ROUND);

                    ImageManagerUtils.getUtils().compressAndPost(GroupInfoEditorActivity.this, headUrl, 300, new ImageManagerUtils.ImageCompressListener() {
                        @Override
                        public void onResult(int stutas, String cover, String result) {
                            if (status == 0 && !TextUtils.isEmpty(result)){
                                //上传成功
                                ossHeadUrl = result;
                            }
                        }
                    });

                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode ==1 && data != null){
            String name = data.getStringExtra(BaseConfig.ORG_NAME);
            switch (requestCode){
                case 0://群昵称
                    editor_name.setText(name);
                    break;
                case 1://群描述
                    editor_introduce.setText(name);
                    break;
            }
            GroupInfoActivity.isDeleteMember = true;

        }
    }
}
